package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.LorealCode;
import cn.com.connext.scrm.repository.master.LorealCodeRepository;
import cn.com.connext.scrm.utils.T;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class LorealCodeService {
    @Autowired
    private LorealCodeRepository lorealCodeRepository;


    public LorealCode checkCode(LorealCode code) {
        return lorealCodeRepository.findByCode(code.getCode());
    }

    public void update(LorealCode codeEntity) {
        codeEntity.setUseDate(ZonedDateTime.now());
        codeEntity.setIsUse(1);
        lorealCodeRepository.saveAndFlush(codeEntity);
    }

    public void batchAdd() {
        LorealCode code = null;
        List<LorealCode> list = new ArrayList<>();
        for (int i=0;i <600;i++){
            code =new LorealCode();
            code.setCode(T.generateRandomArray(6));
            code.setIsUse(2);
            list.add(code);
        }
        lorealCodeRepository.save(list);
    }
}
